package com.boilerCloud.service.msg;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.boilerCloud.dao.msg.PushDao;
import com.boilerCloud.dao.user.AppUserDao;
import com.boilerCloud.dto.HzlqswPage;
import com.boilerCloud.dto.device.DeviceInfo;
import com.boilerCloud.dto.msg.AppMessages;
import com.boilerCloud.dto.msg.PushMessages;
import com.boilerCloud.dto.user.AppUser;
import com.boilerCloud.service.user.IAppUserService;
@Service
public class PushServiceImpl implements IPushService {

	@Resource
	private PushDao dao;
	
	@Resource
	private AppUserDao userdao;
	@Override
	public void addPushMessages(PushMessages msg) throws Exception {
		dao.save(msg);
		int msg_id  = msg.getId();
		String[] recevids = msg.getReceiveIds().split(",");
		for (int i = 0; i < recevids.length; i++) {
			
			dao.addPushRecord(msg_id,Integer.parseInt(recevids[i]));
		}
	}
	

	@Override
	public void qryList(PushMessages device, HzlqswPage<PushMessages> page)
			throws Exception {
		dao.qryList(device,page);
		List<PushMessages> list = page.getPageList();
		if(list != null){
			for (int j = 0; j < list.size(); j++) {
				//System.out.println(dao.getReceiveName(list.get(j).getId()));
				list.get(j).setReceiveName(String.join(" ",dao.getReceiveName(list.get(j).getId())));
			}
		}
		
	}

	@Override
	public void replyFeedbackMsg(PushMessages msg)throws Exception {
		dao.replyFeedbackMsg(msg);
		
	}

	@Override
	public void qryFeedbackMsgList(AppMessages msg, HzlqswPage<AppMessages> page)throws Exception  {
		dao.qryFeedbackMsgList(msg,page);
		
	}


	@Override
	public List<AppUser> getReceiverList() throws Exception {
		 List<AppUser> list = userdao.getReceiverList();
		 
		 Map<Integer,List<AppUser>> map = list.stream().collect(Collectors.groupingBy(AppUser::getDealerId));
		 list = list.stream()
				 .filter(a -> map.containsKey(a.getId()) || a.getUserType() == 1)
		 .collect(Collectors.toList());
		return list;
	}


	@Override
	public void del(int id) throws Exception {
		dao.del(id);
	}

}
